package net.nagaseyasuhito.nicola.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

@MappedSuperclass
public abstract class BaseEntity implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column(nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date createTimestamp;

	@Column(nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	@Version
	private Date updateTimestamp;

	@PrePersist
	protected void onPrePersist() {
		this.createTimestamp = new Date();
	}
	
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getCreateTimestamp() {
		return createTimestamp;
	}

	public Date getUpdateTimestamp() {
		return updateTimestamp;
	}

	@Override
	public int hashCode() {
		//FIXME: 手抜きXD
		return this.getId() == null?super.hashCode():(int)(long)this.getId();
	}

	@Override
	public boolean equals(Object object) {
		//FIXME: 手抜きXD
		if(!this.getClass().isInstance(object)) {
			return false;
		}

		BaseEntity entity = (BaseEntity) object;
		if(this.getId() == null || entity.getId() == null) {
			return false;
		}

		return this.getId().equals(entity.getId());
	}
}
